if(NOT DEFINED Directory)
    message(SEND_ERROR "Directory has to be defined")
endif()

if(NOT DEFINED ContentFile)
    message(SEND_ERROR "ContentFile has to be defined")
endif()

if(NOT DEFINED Name)
    message(SEND_ERROR "Name has to be defined")
endif()

include(SBE/helpers/ArgumentParser)
include(SBE/helpers/ContextParser)
include(SBE/PackageExporter)

sbeLoadContextFile(${ContentFile})
sbeGetPackageProperties(${Name})

set(extensions "")
set(bugfixes "")
set(extensionStartRegexp "^[ \t]+<extensions>[ \t]*$")
set(bugfixesStartRegexp "^[ \t]+<bugfixes>[ \t]*$")
set(issueStartRegexp "^[ \t]+\\+?[ \t]*<jira[ \t]+.*>[ \t]*")
set(issueEndRegexp "^[ \t]+\\+?[ \t]*<jira[ \t]+.*>[ \t]*")
set(sectionEndRegexp "^[ \t]+<[a-z]+>[ \t]*$")
set(releaseRegexp "^.+[ \t]\\(.+\\)$")
set(releaseEndRegexp ${releaseRegexp})

# get issue data from Releases notes of all packages
foreach(dep ${OverallDependencies})
    sbeGetPackageLocalPath(${dep} path)
    set(ReleaseNotes "${path}/ReleaseNotes")
    if (EXISTS ${ReleaseNotes})
        set(releaseNameToStop "")
        file(STRINGS ${ReleaseNotes} context)
        
        set(inExtensionSection no)
        set(inBugfixSection no)
        set(inIssueSection no)
        set(releaseName "unknown")
        set(issueName "")
        set(issueKey "")
        set(issueContext "")
        foreach(line ${context})
            # Is issue context end. It can ends by start of next release, start of next section or next isuue
            if (inIssueSection AND ("${line}" MATCHES ${issueEndRegexp} OR "${line}" MATCHES ${sectionEndRegexp} OR "${line}" MATCHES ${releaseEndRegexp}))
                if(inBugfixSection)
                    list(APPEND bugfixes ${issueKey})
                    list(REMOVE_DUPLICATES bugfixes)
                elseif(inExtensionSection)
                    list(APPEND extensions ${issueKey})
                    list(REMOVE_DUPLICATES extensions)
                endif()
                
                if(NOT DEFINED ${issueKey}_${dep}_releases)
                    set(${issueKey}_${dep}_releases ${releaseName})
                else()
                    list(APPEND ${issueKey}_${dep}_releases ${releaseName})
                endif()
                
                set(${issueKey}_${dep}_${releaseName}_issueContext ${issueContext})
                set(inIssueSection no)
                set(issueName "")
                set(issueKey "")
                set(issueContext "")
            endif()
            
            if("${line}" MATCHES ${releaseRegexp})
                string(REGEX MATCH "^([^ \t]+)" relName "${line}")
                set(releaseName ${CMAKE_MATCH_1})
                if("${releaseNameToStop}" STREQUAL "${releaseName}")
                    break()
                endif()
            endif()
            
            if (inExtensionSection AND "${line}" MATCHES ${sectionEndRegexp})
                set(inExtensionSection no)
            endif()
            
            if (inBugfixSection AND "${line}" MATCHES ${sectionEndRegexp})
                set(inBugfixSection no)
            endif()
            
            if("${line}" MATCHES ${extensionStartRegexp})
                set(inExtensionSection yes)
            endif()
                
            if("${line}" MATCHES ${bugfixesStartRegexp})
                set(inBugfixSection yes)
            endif()
               

            if(inIssueSection)
                list(APPEND issueContext ${line})
            endif()
            
            if("${line}" MATCHES ${issueStartRegexp})
                # get issue key
                string(REGEX MATCH "<jira[ \t]+(.+)>" key "${line}")
                set(issueKey ${CMAKE_MATCH_1})
                #get issue name
                string(REGEX MATCH "<jira .*>[ \t](.*)$" name "${line}")
                set(${issueKey}_issueName ${CMAKE_MATCH_1})
                if (NOT DEFINED ${issueKey}_issuePackages)
                    set(${issueKey}_issuePackages ${dep})
                else()
                    list(APPEND ${issueKey}_issuePackages ${dep})
                    list(REMOVE_DUPLICATES ${issueKey}_issuePackages)
                endif()
                set(inIssueSection yes)
            endif()
        endforeach()
    endif()
endforeach()

# generate issues content
set(Ident "    ")
set(content "")

if(NOT "${extensions}" STREQUAL "")
    string(CONCAT content "${content}" "${Ident}<extensions>\n")
    foreach(ext ${extensions})
        # issue heading
        set(issueIdText "${Ident}${Ident}+ <jira ${ext}>")
        string(CONCAT content ${content} "${issueIdText} ${${ext}_issueName}\n")
        string(LENGTH "${issueIdText}" issueIdLen)
        set(issueContentIdent "")
        foreach(n RANGE ${issueIdLen})
            string(CONCAT issueContentIdent ${issueContentIdent} " ")
        endforeach()
        # issue content
        foreach(dep ${${ext}_issuePackages})
            foreach(rel ${${ext}_${dep}_releases})
                string(REGEX MATCH "Developers?[ \t]+([^;]+)" temp "${${ext}_${dep}_${rel}_issueContext}")
                set(developers ${CMAKE_MATCH_1})
                string(CONCAT content ${content} "${issueContentIdent}Developed in ${dep} (${rel}) by ${developers}\n")
            endforeach()
        endforeach()
    endforeach()
endif()

if(NOT "${bugfixes}" STREQUAL "")
    string(CONCAT content "${content}" "${Ident}<bugfixes>\n")
    foreach(ext ${bugfixes})
        # issue heading
        set(issueIdText "${Ident}${Ident}+ <jira ${ext}>")
        string(CONCAT content ${content} "${issueIdText} ${${ext}_issueName}\n")
        string(LENGTH "${issueIdText}" issueIdLen)
        set(issueContentIdent "")
        foreach(n RANGE ${issueIdLen})
            string(CONCAT issueContentIdent ${issueContentIdent} " ")
        endforeach()
        # issue content
        foreach(dep ${${ext}_issuePackages})
            foreach(rel ${${ext}_${dep}_releases})
                string(REGEX MATCH "Developers?[ \t]+([^;]+)" temp "${${ext}_${dep}_${rel}_issueContext}")
                set(developers ${CMAKE_MATCH_1})
                string(CONCAT content ${content} "${issueContentIdent}Developed in ${dep} (${rel}) by ${developers}\n")
            endforeach()
        endforeach()
    endforeach()
endif()

message("${content}")
    
foreach(bug ${bugfixes})
endforeach()

